﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Security;

namespace SBPweb.DNN.PhotoGallery
{
    public partial class PhotoGalleryViewer : DotNetNuke.Entities.Modules.PortalModuleBase, DotNetNuke.Entities.Modules.IActionable
    {
        
        #region Private fields

        private const string MODULE_FOLDER_ROOT = "/DesktopModules/SBPweb_PhotoGallery";

        private ButtonOptions pButtonOption = ButtonOptions.BUTTON_PLAYPAUSE | ButtonOptions.BUTTON_PREVNEXT | ButtonOptions.BUTTON_PREVNEXTPAGE | ButtonOptions.BUTTON_FULLSCREEN | ButtonOptions.BUTTON_SAVE;
        private string strCookie;

        private PhotoGalleryInfo pSlides;

        #endregion

        #region Constructor logic

        public PhotoGalleryViewer()
            : base()
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                InitScripts();
                strCookie = ("_Module" + (ModuleId.ToString() + "_PhotoGalleryViewer"));
                if (!Page.IsPostBack)
                {
                    InitCookies();
                    InitGallery();
                }
            }
            catch (Exception exc)
            {
                // Module failed to load
                Exceptions.ProcessModuleLoadException(this, exc);
            }
        }

        #endregion

        #region Property accessors

        public DotNetNuke.Entities.Modules.Actions.ModuleActionCollection ModuleActions
        {
            get
            {
                DotNetNuke.Entities.Modules.Actions.ModuleActionCollection Actions = new
                DotNetNuke.Entities.Modules.Actions.ModuleActionCollection();
                Actions.Add(GetNextActionID(),
                Localization.GetString(DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent,
                LocalResourceFile), DotNetNuke.Entities.Modules.Actions.ModuleActionType.AddContent, "", "",
                EditUrl(), false, SecurityAccessLevel.Edit, true, false);
                return Actions;
            }
        }

        public PhotoGalleryInfo Slides
        {
            get
            {
                return pSlides;
            }
            set
            {
                pSlides = value;
            }
        }

        public Unit Width
        {
            get
            {
                if (pSlides == null || pSlides.Width == null)
                {
                    return 640;
                }
                return Unit.Parse(pSlides.Width);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.Width = value.ToString();
            }
        }

        public Unit Height
        {
            get
            {
                if (pSlides == null || pSlides.Height == null)
                {
                    return 480;
                }
                return Unit.Parse(pSlides.Height);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.Height = value.ToString();
            }
        }

        public Color Background
        {
            get
            {
                if (pSlides == null || pSlides.Background == null)
                {
                    return Color.DarkGray;
                }
                return GetColorFromName(pSlides.Background);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.Background = GetNameFromColor(value);
            }
        }

        public Color Foreground
        {
            get
            {
                if (pSlides == null || pSlides.Foreground == null)
                {
                    return Color.LightGray;
                }
                return GetColorFromName(pSlides.Foreground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.Foreground = GetNameFromColor(value);
            }
        }

        public Color ButtonBackground
        {
            get
            {
                if (pSlides == null || pSlides.ButtonBackground == null)
                {
                    return Color.Black;
                }
                return GetColorFromName(pSlides.ButtonBackground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ButtonBackground = GetNameFromColor(value);
            }
        }

        public Color ButtonForeground
        {
            get
            {
                if (pSlides == null || pSlides.ButtonForeground == null)
                {
                    return Color.White;
                }
                return GetColorFromName(pSlides.ButtonForeground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ButtonForeground = GetNameFromColor(value);
            }
        }

        public Color ButtonBorder
        {
            get
            {
                if (pSlides == null || pSlides.ButtonBorder == null)
                {
                    return Color.White;
                }
                return GetColorFromName(pSlides.ButtonBorder);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ButtonBorder = GetNameFromColor(value);
            }
        }

        public Color ButtonPlayForeground
        {
            get
            {
                if (pSlides == null || pSlides.ButtonPlayForeground == null)
                {
                    return Color.LightGreen;
                }
                return GetColorFromName(pSlides.ButtonPlayForeground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ButtonPlayForeground = GetNameFromColor(value);
            }
        }

        public Color CaptionBackground
        {
            get
            {
                if (pSlides == null || pSlides.CaptionBackground == null)
                {
                    return Color.Black;
                }
                return GetColorFromName(pSlides.CaptionBackground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.CaptionBackground = GetNameFromColor(value);
            }
        }

        public Color CaptionForeground
        {
            get
            {
                if (pSlides == null || pSlides.CaptionForeground == null)
                {
                    return Color.White;
                }
                return GetColorFromName(pSlides.CaptionForeground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.CaptionForeground = GetNameFromColor(value);
            }
        }

        public Color ContentBackground
        {
            get
            {
                if (pSlides == null || pSlides.ContentBackground == null)
                {
                    return Color.Black;
                }
                return GetColorFromName(pSlides.ContentBackground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ContentBackground = GetNameFromColor(value);
            }
        }

        public Color SelectedThumbnailBorder
        {
            get
            {
                if (pSlides == null || pSlides.SelectedThumbnailBorder == null)
                {
                    return Color.Yellow;
                }
                return GetColorFromName(pSlides.SelectedThumbnailBorder);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.SelectedThumbnailBorder = GetNameFromColor(value);
            }
        }

        public Color TrackerForeground
        {
            get
            {
                if (pSlides == null || pSlides.TrackerForeground == null)
                {
                    return Color.Silver;
                }
                return GetColorFromName(pSlides.TrackerForeground);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.TrackerForeground = GetNameFromColor(value);
            }
        }

        public decimal CaptionOpacity
        {
            get
            {
                if (pSlides == null || !pSlides.CaptionOpacity.HasValue)
                {
                    return 0.7M;
                }
                return pSlides.CaptionOpacity.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.CaptionOpacity = value;
            }
        }

        public Font CaptionFont
        {
            get
            {
                if (pSlides == null || pSlides.CaptionFont == null)
                {
                    return new Font(new FontFamily("Verdana"), 11, FontStyle.Regular);
                }
                return GetFontFromName(pSlides.CaptionFont);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.CaptionFont = GetNameFromFont(value);
            }
        }

        public Font TrackerFont
        {
            get
            {
                if (pSlides == null || pSlides.TrackerFont == null)
                {
                    return new Font(new FontFamily("Verdana"), 11, FontStyle.Regular);
                }
                return GetFontFromName(pSlides.TrackerFont);
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.TrackerFont = GetNameFromFont(value);
            }
        }

        public int BorderRadius
        {
            get
            {
                if (pSlides == null || !pSlides.BorderRadius.HasValue)
                {
                    return 8;
                }
                return pSlides.BorderRadius.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.BorderRadius = value;
            }
        }

        public int Padding
        {
            get
            {
                if (pSlides == null || !pSlides.Padding.HasValue)
                {
                    return 8;
                }
                return pSlides.Padding.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.Padding = value;
            }
        }

        public bool CaptionVisible
        {
            get
            {
                if (pSlides == null || !pSlides.CaptionVisible.HasValue)
                {
                    return true;
                }
                return pSlides.CaptionVisible.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.CaptionVisible = value;
            }
        }

        public bool ThumbnailsVisible
        {
            get
            {
                if (pSlides == null || !pSlides.ThumbnailsVisible.HasValue)
                {
                    return true;
                }
                return pSlides.ThumbnailsVisible.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.ThumbnailsVisible = value;
            }
        }

        public bool TrackerVisible
        {
            get
            {
                if (pSlides == null || !pSlides.TrackerVisible.HasValue)
                {
                    return true;
                }
                return pSlides.TrackerVisible.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.TrackerVisible = value;
            }
        }

        public bool AutoPlay
        {
            get
            {
                if (pSlides == null || !pSlides.AutoPlay.HasValue)
                {
                    return true;
                }
                return pSlides.AutoPlay.Value;
            }
            set
            {
                if (pSlides == null)
                {
                    return;
                }
                pSlides.AutoPlay = value;
            }
        }

        public ButtonOptions ButtonOption
        {
            get
            {
                return pButtonOption;
            }
            set
            {
                pButtonOption = value;
            }
        }

        public string ImageFolder
        {
            get
            {
                return Page.ResolveUrl(string.Format("~{0}/Images/", MODULE_FOLDER_ROOT));
            }
        }

        #endregion

        #region Methods

        public string GetNameFromColor(Color color)
        {
            string ret = ColorTranslator.ToHtml(color);
            if (ret.Contains("Grey"))
            {
                ret = ret.Replace("Grey", "Gray");
            }
            return ret;            
        }

        public Color GetColorFromName(string color)
        {
            return ColorTranslator.FromHtml(color);
        }

        public string GetButtonOptions(ButtonOptions options)
        {
            return options.ToString().Replace(",", " |");
        }

        public string GetBoolean(bool boolean)
        {
            return boolean.ToString().ToLower();
        }

        public string GetFontFamily(Font font)
        {
            return font.FontFamily.Name;
        }

        public string GetFontSize(Font font)
        {
            return font.Size.ToString();
        }

        public string GetFontStyle(Font font)
        {
            switch (font.Style)
            {
                default:
                case FontStyle.Regular:
                    return "Normal";
                case FontStyle.Italic:
                    return "Italic";
            }
        }

        public string GetFontWeight(Font font)
        {
            if (font.Bold)
            {
                return "Bold";
            }
            return "Normal";
        }

        public Font GetFontFromName(string font)
        {
            FontFamily family = null;
            FontUnit size = FontUnit.Empty;
            FontStyle style = FontStyle.Regular;
            string[] split = font.Split(new char[] { '[', ']', ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in split)
            {
                string[] spl = s.Trim().Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                switch (spl[0])
                {
                    default:
                        break;
                    case "Font: Name":
                        family = new FontFamily(spl[1]);
                        break;
                    case "Size":
                        size = new FontUnit(spl[1]);
                        break;
                    case "Style":
                        style = (FontStyle)Enum.Parse(typeof(FontStyle), spl[1]);
                        break;
                }
            }
            return new Font(family, (float)size.Unit.Value, style);
        }

        public string GetNameFromFont(Font font)
        {
            string s = font.ToString();
            s = s.Insert(s.Length - 1, string.Format(", Style={0}", font.Style));
            return s;
        }

        protected void InitScripts()
        {
            Page.ClientScript.RegisterClientScriptInclude("Silverlight", Page.ResolveUrl(string.Format("~{0}/Scripts/Silverlight.js", MODULE_FOLDER_ROOT)));
            Page.ClientScript.RegisterClientScriptInclude("SilverlightSpy", Page.ResolveUrl(string.Format("~{0}/Scripts/SilverlightSpy.js", MODULE_FOLDER_ROOT)));
            Page.ClientScript.RegisterClientScriptInclude("Slide", Page.ResolveUrl(string.Format("~{0}/Scripts/Slide.js", MODULE_FOLDER_ROOT)));
            Page.ClientScript.RegisterClientScriptInclude("Slideshow", Page.ResolveUrl(string.Format("~{0}/Scripts/Slideshow.js", MODULE_FOLDER_ROOT)));
            Page.ClientScript.RegisterClientScriptInclude("SlideshowCreate", Page.ResolveUrl(string.Format("~{0}/Scripts/SlideshowCreate.js", MODULE_FOLDER_ROOT)));
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "sceneSourceXmlPrefix", string.Format("sceneSourceXmlPrefix = '{0}/';", Page.ResolveUrl("~" + MODULE_FOLDER_ROOT)), true);
        }

        protected void InitCookies()
        {
            if (UserInfo.Membership != null)
            {
                HttpCookie cookie = new HttpCookie("username");
                cookie.Expires = DateTime.Now.AddHours(1);
                cookie.Domain = PortalSettings.PortalAlias.HTTPAlias;
                cookie.Value = UserInfo.Membership.Username;
                Response.Cookies.Add(cookie);
            }
        }

        protected void InitGallery()
        {
            Slides = PhotoGalleryController.GetPhotoGallery(null, ModuleId);
            if (Slides != null)
            {
                Slides.AddRange(PhotoGallerySlideController.GetPhotoGallerySlides(Slides.PhotoGalleryId));
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(string.Format("<script id=\"{0}_slides\" type=\"text/xml\">", this.ClientID));
            sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            if (Slides != null)
            {
                Label l = new Label();
                l.Text = Slides.Title;
                pTitle.Controls.Add(l);
                Slides.BasePath = string.Format("{0}{1}/", PortalSettings.HomeDirectory, Slides.BasePath);
                sb.AppendLine(Slides.ToString());
            }
            sb.AppendLine("</script>");
            litSlides.Text = sb.ToString();
            base.OnPreRender(e);
        }

        #region Event handlers

        protected void bRefresh_Click(object sender, EventArgs e)
        {
            //Slides = pSlides.Clone(0, ++pI);
        }

        #endregion

        #endregion

        #region Subclasses, enums

        [Flags()]
        public enum ButtonOptions
        {
            BUTTON_PLAYPAUSE = 0,
            BUTTON_PREVNEXT = 1,
            BUTTON_PREVNEXTPAGE = 2,
            BUTTON_FULLSCREEN = 4,
            BUTTON_SAVE = 8
        }

        #endregion

    }
}